<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{margin: 0;padding: 0;}
        #div1{
            width: 100px;height: 100px;background: red;position: absolute;right: 0;
        }
    </style>
    <script>
        window.onload = function(){
            var div1 = document.getElementById("div1");
            var scrollTop = document.documentElement.scrollTop||document.body.scrollTop;
            var windowHeight = document.documentElement.clientHeight||document.body.clientHeight;

            var ih = parseInt(scrollTop + (windowHeight - div1.offsetHeight)/2);
            startMove(ih);
        }
        window.onscroll = function(){
            var div1 = document.getElementById("div1");
            var scrollTop = document.documentElement.scrollTop||document.body.scrollTop;
            var windowHeight = document.documentElement.clientHeight||document.body.clientHeight;

            var ih = parseInt(scrollTop + (windowHeight - div1.offsetHeight)/2);
            startMove(ih);
        }
        var timer = null;
        function startMove(itarget){
            clearInterval(timer);
            timer = setInterval(function(){
                var speed = (itarget - div1.offsetTop)/8;
                speed = speed>0?Math.ceil(speed):Math.floor(speed);
                if(div1.offsetTop==itarget){
                    clearInterval(timer);
                }else{
                    div1.style.top = speed + div1.offsetTop + 'px';
                }
                
            },30);
        }
    </script>
</head>
<body style='height:3000px'>
    <div id="div1"></div>
</body>
</html>